package wowraidmanager.client.wishlist.wishlist;

import java.util.ArrayList;

import wowraidmanager.client.service.wishlist.MemberService;
import wowraidmanager.client.service.wishlist.MemberServiceAsync;
import wowraidmanager.client.service.wishlist.RaidbossDroplistService;
import wowraidmanager.client.service.wishlist.RaidbossDroplistServiceAsync;
import wowraidmanager.client.wishlist.utils.RaidbossDroplistUtil;
import wowraidmanager.shared.model.wishlist.Member;
import wowraidmanager.shared.model.wishlist.RaidbossDroplist;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;

public class EditwishlistGameVersion extends Composite{

    private static EditwishlistUiBinder uiBinder = GWT.create(EditwishlistUiBinder.class);

    protected RaidbossDroplistServiceAsync raidbossDroplistService = GWT.create(RaidbossDroplistService.class);

    protected MemberServiceAsync memberService = GWT.create(MemberService.class);

    interface EditwishlistUiBinder extends UiBinder<Widget, EditwishlistGameVersion>{
    }

    UserStatus userStatus;

    ArrayList<RaidbossDroplist> raidbossDroplist;

    ArrayList<Member> currentMemberList;

    Member currentMember;

    @UiField
    TabPanel gameVersionTab;

    @UiField
    Label title;

    @UiField
    ListBox userListBox;

    public EditwishlistGameVersion(UserStatus userStatus){
        initWidget(uiBinder.createAndBindUi(this));
        this.userStatus = userStatus;
        setTitle();
        getRaidbossdroplist();
        setListBox();
    }

    private void setListBox(){
        userListBox.addChangeHandler(new ChangeHandler(){

            public void onChange(ChangeEvent event){
                getUserListBox();
            }
        });
    }

    private void setTitle(){
        title.setText(userStatus.getMember().getMemberName() + "'s Wishlist");

    }

    private void getRaidbossdroplist(){
        raidbossDroplistService.getRaidbossDroplist(new AsyncCallback<ArrayList<RaidbossDroplist>>(){

            public void onSuccess(ArrayList<RaidbossDroplist> result){
                raidbossDroplist = result;
                getcurrentMember();
            }

            public void onFailure(Throwable caught){
                // TODO 自動生成されたメソッド・スタブ

            }
        });

    }

    protected void getcurrentMember(){
        memberService.getMember(userStatus.getMember().getMemberAccount(), new AsyncCallback<ArrayList<Member>>(){

            public void onSuccess(ArrayList<Member> result){
                currentMemberList = result;
                setUserListBox();
            }

            public void onFailure(Throwable caught){
                // TODO 自動生成されたメソッド・スタブ

            }
        });
    }

    protected void setUserListBox(){
        for(Member member:currentMemberList){
            if(member.getMembertype().equals("Officer") || member.getMembertype().equals("Member")){
                userListBox.addItem(member.getMemberName());
            }
        }

        for(Member member:currentMemberList){
            if(member.getMembertype().equals("SubCharacter")){
                userListBox.addItem(member.getMemberName());
            }
        }

        getUserListBox();
    }

    protected void getUserListBox(){
        String nameListBox = userListBox.getItemText(userListBox.getSelectedIndex());
        for(Member member:currentMemberList){
            if(nameListBox.equals(member.getMemberName())){
                currentMember = member;
            }
        }
        setGameversionTab();
    }

    protected void setGameversionTab(){
        gameVersionTab.clear();
        ArrayList<String> gameVersionList = RaidbossDroplistUtil.getGameversion(raidbossDroplist);

        for(String gameVersion:gameVersionList){
            ArrayList<RaidbossDroplist> newRaidbossdroplist = RaidbossDroplistUtil.getRaidbossdroplistGameVersion(raidbossDroplist, gameVersion);
            gameVersionTab.add(new EditwishlistRaidzone(gameVersion, userStatus, newRaidbossdroplist, currentMember), gameVersion);
        }
        gameVersionTab.selectTab(0);
    }
}
